<!--
  * 外包公司信息
  *
  * @Author:    zzw
  * @Date:      2024-09-23 10:25:55
  * @Copyright  jz
-->
<template>
  <a-modal :title="form.id ? '编辑' : '添加'" width="800px" :open="visibleFlag" @cancel="onClose" :maskClosable="false"
    :destroyOnClose="true">
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 10 }">

      <a-row>
        <a-col :span="12">
          <a-form-item label="单位名称" name="name">
            <a-input style="width: 100%" v-model:value="form.name" :disabled="disabled" placeholder="单位名称" />
          </a-form-item>
        </a-col>
        <a-col :span="12" v-if="disabled">
          <a-form-item label="单位编码" name="code">
            <a-input style="width: 100%" v-model:value="form.code" :disabled="disabled" placeholder="单位编码" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="注册地址" name="registrationAddress">
            <a-textarea v-model:value="form.registrationAddress" :disabled="disabled" placeholder="注册地址" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="注册资本" name="registeredCapital">
            <a-input-number style="width: 100%" :disabled="disabled" v-model:value="form.registeredCapital"
              placeholder="注册资本，假设最大为十亿，保留两位小数" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="成立时间" name="establishmentDate">
            <a-date-picker valueFormat="YYYY-MM-DD" :disabled="disabled" v-model:value="form.establishmentDate"
              style="width: 100%" placeholder="成立时间" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="营业执照编号" name="businessLicenseNum">
            <a-input style="width: 100%" :disabled="disabled" v-model:value="form.businessLicenseNum"
              placeholder="营业执照编号" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="联系电话" name="contactPhone">
            <a-input style="width: 100%" :disabled="disabled" v-model:value="form.contactPhone" placeholder="联系电话" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="传真号码" name="faxNumber">
            <a-input style="width: 100%" :disabled="disabled" v-model:value="form.faxNumber" placeholder="传真号码" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="电子邮箱" name="email">
            <a-input style="width: 100%" :disabled="disabled" v-model:value="form.email" placeholder="电子邮箱" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="开户行" name="openingBank">
            <a-input style="width: 100%" :disabled="disabled" v-model:value="form.openingBank" placeholder="开户行" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="收款账户" name="receiverAccount">
            <a-input style="width: 100%" :disabled="disabled" v-model:value="form.receiverAccount" placeholder="收款账户" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="公司类型" name="companyType">
            <a-select v-model:value="form.companyType" style="width: 100%"  placeholder="公司类型">
              <a-select-option value="无限责任公司">无限责任公司</a-select-option>
              <a-select-option value="有限责任公司">有限责任公司</a-select-option>
              <a-select-option value="股份有限公司">股份有限公司</a-select-option>
              <a-select-option value="上市公司">上市公司</a-select-option>
              <a-select-option value="一人有限责任公司">一人有限责任公司</a-select-option>
              <a-select-option value="国有独资公司">国有独资公司</a-select-option>
              <a-select-option value="股份两合公司">股份两合公司</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>

        <a-col :span="12">
          <a-form-item label="官方网站" name="website">
         
            <a-input style="width: 100%" :disabled="disabled" v-model:value="form.website" placeholder="官方网站" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="法定代表人姓名" name="legalRepresentativeName">
            <a-input style="width: 100%" :disabled="disabled" v-model:value="form.legalRepresentativeName"
              placeholder="法定代表人姓名" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="法定代表人联系方式" name="legalRepresentativePhone">
            <a-input style="width: 100%" :disabled="disabled" v-model:value="form.legalRepresentativePhone"
              placeholder="法定代表人联系方式" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="开关" name="status">
            <a-switch :checkedValue="1" :disabled="disabled" :unCheckedValue="0" checked-children="合作"
              un-checked-children="不合作" v-model:checked="form.status" />
          </a-form-item>

        </a-col>
        <a-col :span="12">
          <a-form-item label="备注" name="remark">
            <a-textarea :disabled="disabled" v-model:value="form.remark" placeholder="备注" allow-clear />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button v-if="!disabled" type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-modal>
</template>
<script setup>
import { reactive, ref, nextTick } from 'vue';
import _ from 'lodash';
import { message } from 'ant-design-vue';
import { SmartLoading } from '/@/components/framework/smart-loading';
import { outsourcingCompanyApi } from '/@/api/Outsourcing/basicinformation/index';
import { smartSentry } from '/@/lib/smart-sentry';
import BooleanSelect from '/@/components/framework/boolean-select/index.vue';

// ------------------------ 事件 ------------------------

const emits = defineEmits(['reloadList']);

// ------------------------ 显示与隐藏 ------------------------
// 是否显示
const visibleFlag = ref(false);
const disabled = ref(false)
function show(rowData, d) {
  if (d && d == 'detail') {
    disabled.value = true;
  } else {
    disabled.value = false;
  }
  Object.assign(form, formDefault);
  if (rowData && !_.isEmpty(rowData)) {
    Object.assign(form, rowData);
  }
  visibleFlag.value = true;
  nextTick(() => {
    formRef.value.clearValidate();
  });
}

function onClose() {
  Object.assign(form, formDefault);
  visibleFlag.value = false;
}

// ------------------------ 表单 ------------------------

// 组件ref
const formRef = ref();

const formDefault = {
  id: undefined,
  name: undefined, //单位名称
  registrationAddress: undefined, //注册地址
  registeredCapital: undefined, //注册资本，假设最大为十亿，保留两位小数
  establishmentDate: undefined, //成立时间
  businessLicenseNum: undefined, //营业执照编号
  contactPhone: undefined, //联系电话
  faxNumber: undefined, //传真号码
  email: undefined, //电子邮箱
  website: undefined, //官方网站
  legalRepresentativeName: undefined, //法定代表人姓名
  legalRepresentativePhone: undefined, //法定代表人联系方式
  status: undefined, //0关闭，1开启
  remark: undefined, //备注
  openingBank: undefined,
  receiverAccount: undefined,
  companyType:undefined
};

let form = reactive({ ...formDefault });

const rules = {
  name: [{ required: true, message: '单位名称 必填' }],
  registrationAddress: [{ required: true, message: '注册地址 必填' }],
  registeredCapital: [{ required: true, message: '注册资本，假设最大为十亿，保留两位小数 必填' }],
  establishmentDate: [{ required: true, message: '成立时间 必填' }],
  businessLicenseNum: [{ required: true, message: '营业执照编号 必填' }],
  contactPhone: [{ required: true, message: '联系电话 必填' }],
  email: [{ required: true, message: '电子邮箱 必填' }],
  legalRepresentativeName: [{ required: true, message: '法定代表人姓名 必填' }],
  legalRepresentativePhone: [{ required: true, message: '法定代表人联系方式 必填' }],
  openingBank: [{ required: true, message: '开户行 必填' }],
  receiverAccount: [{ required: true, message: '收款账户 必填' }],
  companyType: [{ required: true, message: '公司类型 必填' }],
};

// 点击确定，验证表单
async function onSubmit() {
  try {
    await formRef.value.validateFields();
    save();
  } catch (err) {
    message.error('参数验证错误，请仔细填写表单数据!');
  }
}

// 新建、编辑API
async function save() {
  if (disabled.value) {
    onClose();
    return
  }
  SmartLoading.show();
  try {
    if (form.id) {
      await outsourcingCompanyApi.update(form);
    } else {
      await outsourcingCompanyApi.add(form);
    }
    message.success('操作成功');
    emits('reloadList');
    onClose();
  } catch (err) {
    smartSentry.captureError(err);
  } finally {
    SmartLoading.hide();
  }
}

defineExpose({
  show,
});
</script>